package manager.action;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.struts2.ServletActionContext;
import org.apache.struts2.json.annotations.JSON;
import org.junit.Test;

import manager.dao.proxy.FriendDAOProxy;
import manager.vo.Friend;
import manager.vo.User;

import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;

public class FriendAction extends ActionSupport implements ModelDriven<Friend>{
	private static final long serialVersionUID = 1L;
    public Friend friend = new Friend();
	@Override
	public Friend getModel() {
		// TODO Auto-generated method stub
		return this.friend;
	}
	public List<Friend> list = null;
	
	public List<Friend> getList() {
		return list;
	}
	public void setList(List<Friend> list) {
		this.list = list;
	}
	//分页参数
	public int start = 0;
	public int end = 0;
	
    public int getStart() {
		return start;
	}
	public void setStart(int start) {
		this.start = start;
	}
	public int getEnd() {
		return end;
	}
	public void setEnd(int end) {
		this.end = end;
	}
	public String returnMsg = "";
	
	public String getReturnMsg() {
		return returnMsg;
	}
	public void setReturnMsg(String returnMsg) {
		this.returnMsg = returnMsg;
	}
	//查询所有好友
	public String getFriend() throws Exception{
		Map<String,Object> map = new HashMap<String,Object>();
		User user = (User)ServletActionContext.getRequest().getSession().getAttribute("user");
		map.put("userid", user.getId());
		map.put("start", start);
		map.put("end", end);
		list = FriendDAOProxy.getInstance().selectFriend(map);
		return "list";
	}
	//添加
	public String addFriend() throws Exception{
		User user = (User)ServletActionContext.getRequest().getSession().getAttribute("user");
		friend.setUserid(user.getId());
		if(FriendDAOProxy.getInstance().addFriend(friend)){
			this.returnMsg = "1";
		}
		else{
			this.returnMsg = "0";
		}
		return "returnMsg";
	}
    //查询条数
	public String selectCount() throws Exception{
		User user = (User)ServletActionContext.getRequest().getSession().getAttribute("user");
		this.returnMsg=String.valueOf(FriendDAOProxy.getInstance().selectCount(user.getId()));
		return "returnMsg";
	}
	//删除
	public String deleteFriend()throws Exception{
		User user = (User)ServletActionContext.getRequest().getSession().getAttribute("user");
		if(FriendDAOProxy.getInstance().deleteFriend(user.getId(), friend.getId())){
			this.returnMsg = "1";
		}else{
			this.returnMsg = "0";
		}
		return "returnMsg";
	}
}
